package com.mili.camera2api;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.params.StreamConfigurationMap ;
import android.media.ImageReader;
import android.content.Context;
import android.util.Range;
import android.util.Size;

public class TestActivity extends AppCompatActivity {
    private ListView mListView;
    private CustomListAdapter aAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        mListView = (ListView) findViewById(R.id.userlist);
        aAdapter = new CustomListAdapter(this);


        mListView.setAdapter(aAdapter);
        try {
             manager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);
             IdList = manager.getCameraIdList();
             cameraId = IdList[0];
             characteristics = manager.getCameraCharacteristics(cameraId);
             test();
        } catch(CameraAccessException ex) {
            add(ex.toString());
        }

    }
    private void add(String t){
	        //METHOD WHICH WILL HANDLE DYNAMIC INSERTION
        aAdapter.add(t);
        aAdapter.notifyDataSetChanged();
    }
    private void test()
    {
	    add("hello");
            list_framerates();
            list_sizes();
            list_ids();
            list_flash_modes();
    }
    private void list_framerates(){
           StringBuilder sb= new StringBuilder();
           sb.append("framerates:");

           Range<Integer>[] fpsRanges = characteristics.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES);
           for(  Range<Integer> r:fpsRanges ){
                sb.append(r.toString()+",");
           }
           add(sb.toString());
   }
    private void list_sizes(){
           StringBuilder sb= new StringBuilder();
           sb.append("previewSizes:");
          StreamConfigurationMap scmap = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
          Size previewSizes[] = scmap.getOutputSizes(ImageReader.class);
           for(  Size r:previewSizes ){
                sb.append(r.toString()+",");
           }
           add(sb.toString());
    }
    private void list_ids(){
           StringBuilder sb= new StringBuilder();
           sb.append("ids:");


           for(  String r:IdList ){
                sb.append(r+",");
           }
           add(sb.toString());
    }
    private void list_flash_modes(){
           StringBuilder sb= new StringBuilder();

           Boolean has_flash_unit= characteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
           add("flash_unit:"+has_flash_unit.toString());
           sb.append("flash_modes:");
           int[] flashModeValues = characteristics.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_MODES);

           for(  int r:flashModeValues  ){
                sb.append(Integer.toString(r)+",");
           }
           add(sb.toString());
    }
    private String [] IdList;
    private CameraCharacteristics characteristics;
    private String cameraId;
    private CameraManager manager;
}